import axios from 'axios';

// 定义 API 基础 URL
const BASE_URL = '/api';

// 获取题库列表（分页+搜索）
export const getQuestionList = (pageNum = 1, pageSize = 10, keyword) => {
    return axios.get(`${BASE_URL}/questions/list`, {
        params: {
            pageNum,
            pageSize,
            keyword
        }
    });
};

// 根据 ID 获取单个题目
export const getQuestionById = (id) => {
    return axios.get(`${BASE_URL}/questions/${id}`);
};

// 获取所有题目列表（不分页）
export const getAllQuestions = () => {
    return axios.get('/question/list');
};

// 分页获取题目列表
export const getQuestionPages = (pageNum = 1, pageSize = 10) => {
    return axios.get('/question/pages', {
        params: {
            pageNum,
            pageSize
        }
    });
};

// 更新题目信息
export const updateQuestionInfo = (questionInfo) => {
    return axios.put('/question/update', questionInfo);
};

// 获取用户收藏题目列表
export const getFavoriteQuestions = (userId, questionId, keyword) => {
    const params = { userId };
    if (questionId) params.questionId = questionId;
    if (keyword) params.keyword = keyword;
    return axios.get(`${BASE_URL}/favorite/list/questions`, { params });
};

// 获取用户收藏详情
export const getFavoriteDetails = (userId) => {
    return axios.get(`${BASE_URL}/favorite/detail/${userId}`);
};

// 添加收藏
export const addFavorite = (userId, questionId) => {
    return axios.post(`${BASE_URL}/favorite/add`, null, {
        params: {
            userId,
            questionId
        }
    });
};

// 取消收藏
export const removeFavorite = (userId, questionId) => {
    return axios.delete(`${BASE_URL}/favorite/remove/${userId}/${questionId}`);
};

// 检查是否已收藏
export const checkFavorite = (userId, questionId) => {
    return axios.get(`${BASE_URL}/favorite/check`, {
        params: {
            userId,
            questionId
        }
    });
};

// 按错题次数降序获取错题列表
export const getWrongQuestionsByCount = (userId) => {
    return axios.get(`${BASE_URL}/wrong-questions/by-count/${userId}`);
};

// 按科目和章节分类获取错题列表
export const getWrongQuestionsBySubjectChapter = (userId) => {
    return axios.get(`${BASE_URL}/wrong-questions/by-subject-chapter/${userId}`);
};

// 按时间排序获取错题列表
export const getWrongQuestionsByTime = (userId, asc = false) => {
    return axios.get(`${BASE_URL}/wrong-questions/by-time/${userId}`, {
        params: { asc }
    });
};

// 更新错题次数
export const updateWrongQuestionCount = (id, newCount) => {
    return axios.post(`${BASE_URL}/wrong-questions/update-count`, null, {
        params: {
            id,
            newCount
        }
    });
};